package com.qyer.search.utils;

import org.apache.commons.lang.StringUtils;

import java.util.*;
/**
 * Created by tangzheng on 16/8/10.
 */
public class DotSplitUtils {

  public static List<Integer> splitToIntList(String source){
    List<Integer> result = new ArrayList<>();

    source = source.replaceAll("([ ]*[，,\\,][ ]*)+", ",");
    if(StringUtils.isNotBlank(source)) {
      String[] tmp = source.split(",");
      for (int i = 0; i < tmp.length; i++) {
        try {
          if(StringUtils.isNotBlank(tmp[i])) {
            int a = Integer.parseInt(tmp[i]);
            result.add(a);
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    return result;
  }

  public static List<Integer> splitToIntList2(String source, int id){
    List<Integer> result = new ArrayList<>();

    source = source.replaceAll("([ ]*[，,\\,][ ]*)+", ",");
    if(StringUtils.isNotBlank(source)) {
      String[] tmp = source.split(",");
      for (int i = 0; i < tmp.length; i++) {
        try {
          if(StringUtils.isNotBlank(tmp[i])) {
            int a = Integer.parseInt(tmp[i]);
            result.add(a);
          }
        } catch (Exception e) {
          //e.printStackTrace();
          System.out.println("id:"+id);
        }
      }
    }
    return result;
  }

  public static List<String> splitToSTRList(String source){
    List<String> result = new ArrayList<>();
    source = source.replaceAll("([ ]*[，,\\,][ ]*)+", ",");
    String[] tmp = source.split(",");
    for(int i = 0; i < tmp.length; i++){
      result.add(tmp[i]);
    }

    return result;
  }

  public static List<Integer> splitToIntList(String source, Map<String, Integer> metaMap){
    List<Integer> result = new ArrayList<>();

    source = source.replaceAll("([ ]*[，,\\,][ ]*)+", ",");
    String[] tmp = source.split(",");
    for(int i = 0; i < tmp.length; i++){
      try {
        if(metaMap.containsKey(tmp[i])){
          result.add(metaMap.get(tmp[i]));
        }
      }catch (Exception e){
        e.printStackTrace();
      }
    }
    return result;
  }
}
